use std::fs;
use std::path::Path;
use obs_sdk::ObsClient;

// 定义测试用的obs四个参数（endpoint, ak, sk, bucket_name）
// 定义测试用的obs四个参数（endpoint, ak, sk, bucket_name）
static ENDPOINT: &str = "obs.cn-north-4.myhuaweicloud.com";
static AK: &str = "YOUR_AK";
static SK: &str = "YOUR_SK";
static BUCKET_NAME: &str = "bucket_name";

#[tokio::test]
async fn test_download_file01() -> Result<(), Box<dyn std::error::Error>> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let data = client.download_object("2hls_stutter-10.mp4").await?;
    let file_path = Path::new("output.mp4");
    match fs::write(file_path, data) {
        Ok(_) => println!("文件保存成功{:?}", file_path),
        Err(e) => eprintln!("文件保存失败:{}", e)
    }
    Ok(())
}

#[tokio::test]
async fn test_download_file02() -> Result<(), Box<dyn std::error::Error>> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let res = client.download_file("2hls_stutter-10.mp4", "video/2hls_stutter-10.mp4", false).await;
    res
}


#[tokio::test]
async fn test_list_prefix() -> Result<(), Box<dyn std::error::Error>> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let res = client.list("tmp").await?;
    println!("{:?}", res);
    Ok(())
}


#[tokio::test]
async fn test_upload_object() -> Result<(), Box<dyn std::error::Error>> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let res = client.upload_file("tmp_cargo.txt", "Cargo.txt").await?;
    println!("{:?}", res);
    Ok(())
}

#[tokio::test]
async fn test_delete_object() -> Result<(), Box<dyn std::error::Error>> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let res = client.delete_object("remote1/.operate/00000000000000_20251017161709.txt").await?;
    println!("{:?}", res);
    Ok(())
}


#[test]
fn test_url_sign() -> Result<(), Box<dyn std::error::Error>> {
    let client = ObsClient {
        endpoint: ENDPOINT.to_string(),
        ak: AK.to_string(),
        sk: SK.to_string(),
        bucket: BUCKET_NAME.to_string(),
    };
    let sign_url = client.url_sign("https://ranfs.obs.cn-north-4.myhuaweicloud.com/tmp_cargo.txt")?;
    println!("sign_url = {}", sign_url);
    Ok(())
}

#[test]
fn test_mime_type() {
    let guess = mime_guess::from_path("a/b/c/some_file.dat");
    println!("mime_type = {:?}", guess.first().unwrap().to_string());
}